// store命名
import { getSystemConfig } from '@/api/system';
import { StoreName } from '@store/store-name';

// 系统模块
export const useSystemStore = defineStore(StoreName.SYSTEM, {
    state: (): System => ({
        purchase: {
            deliveryAddress: [],
        },
        print: {
            // 本公司打印设置 -- 存入公司信息
            OWNprint: {
                // 打印抬头
                companyName: '',
                companyAddress: '',
                contacts: '',
                companyTel: '',
            },
            // 代加工打印设置 -- 存入公司信息
            OEMprint: [],
        },
        config: {
            isCollapse: false,
            isShowHome: true,
            isShowTools: true,
        },
        temp: {
            router: [],
        },
    }),
    actions: {
        // 获取系统配置
        getSystemConfigEvent() {
            return new Promise((_reject) => {
                getSystemConfig().then((res) => {
                    ElMessage.success(res.message);
                    if (res.code == 200) {
                        // 更新本地数据
                        this.purchase = res.data.purchase;
                        this.print = res.data.print;
                    } else {
                        this.purchase = { deliveryAddress: [] };
                        this.print = { OWNprint: { companyName: '', companyAddress: '', contacts: '', companyTel: '' }, OEMprint: [] };
                    }
                });
            });
        },
        // 检测打印设置
        checkPrintSetting(): { message: string; type: 'success' | 'warning' } {
            let message = '';
            let type: 'success' | 'warning' = 'success';
            // 检测own配置
            if (this.print.OWNprint.companyName == '') {
                message += '请设置本公司打印抬头！';
                type = 'warning';
            }
            return { message, type };
        },
    },
    persist: {
        storage: localStorage,
    },
});
